package com.idconfirmsystem.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Toast;

import com.idconfirmsystem.util.CommonHelper.SoftWare.AppInfo;

public class CommonHelper {
	public static class Resource {
		public static String getString(Context c, int rid) {
			return c.getResources().getString(rid);
		}

		public static Integer getInteger(Context c, int rid) {
			String str = c.getResources().getString(rid);
			Integer ret = null;
			try {
				ret = Integer.valueOf(Integer.parseInt(str));
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
			return ret;
		}

		public static String getArrayString(Context c, int rid, int index) {
			return c.getResources().getStringArray(rid)[index];
		}
	}

	public static class Device {
		public static String getPhoneNumber(Context c) {
			TelephonyManager tm = (TelephonyManager) c
					.getSystemService("phone");
			String tel = tm.getLine1Number();
			return tel;
		}

		public static String getImeiNumber(Context c) {
			TelephonyManager tm = (TelephonyManager) c
					.getSystemService("phone");
			String imei = tm.getDeviceId();
			return ((imei == null) ? "000000000000000" : imei);
		}

		public static String getLine1Number(Context c) {
			TelephonyManager tm = (TelephonyManager) c
					.getSystemService("phone");
			String line1Number = tm.getLine1Number();
			return line1Number;
		}

		public static String getLocalMacAddress(Context c) {
			WifiManager wifi = (WifiManager) c.getSystemService("wifi");
			WifiInfo info = wifi.getConnectionInfo();
			return info.getMacAddress();
		}

		public static String getLocalIpAddress(Context c) {
			Enumeration en;
			try {
				for (en = NetworkInterface.getNetworkInterfaces(); en
						.hasMoreElements();) {
					NetworkInterface intf = (NetworkInterface) en.nextElement();
					for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr
							.hasMoreElements();) {
						InetAddress inetAddress = (InetAddress) enumIpAddr
								.nextElement();
						if (!(inetAddress.isLoopbackAddress()))
							return inetAddress.getHostAddress().toString();
					}
				}
			} catch (SocketException ex) {
				Log.e("WifiPreference IpAddress", ex.toString());
			}
			return null;
		}

		public static class Contants {
			public static String sdk = Build.VERSION.SDK;
			public static String model = Build.MODEL;
			public static String release = Build.VERSION.RELEASE;
		}

		public static class Screen {
			public static boolean isHorizontal(Context c) {
				int mCurrentOrientation = c.getResources().getConfiguration().orientation;
				if (mCurrentOrientation == 1)
					return false;

				return (mCurrentOrientation == 2);
			}

			public static int[] getWidthHeight(Activity a) {
				DisplayMetrics dm = new DisplayMetrics();

				a.getWindowManager().getDefaultDisplay().getMetrics(dm);

				int screenWidth = dm.widthPixels;
				int screenHeight = dm.heightPixels;
				int[] ret = { screenWidth, screenHeight };
				return ret;
			}

			public static void setFullscreen(Activity a) {
				setNoTitle(a);
				a.getWindow().setFlags(1024, 1024);
			}

			public static void setNoTitle(Activity a) {
				a.requestWindowFeature(1);
			}
		}

		public static class TelManager {
			private static TelephonyManager tm = null;

			private static void initTelephonyManager(Context c) {
				if (tm == null)
					tm = (TelephonyManager) c.getSystemService("phone");
			}

			public static int getCid(Context c) {
				int cid = -1;
				try {
					initTelephonyManager(c);
					GsmCellLocation gcl = (GsmCellLocation) tm
							.getCellLocation();
					cid = gcl.getCid();
				} catch (Exception e) {
					cid = -1;
				}
				return cid;
			}

			public static int getLac(Context c) {
				int lac = -1;
				try {
					initTelephonyManager(c);
					GsmCellLocation gcl = (GsmCellLocation) tm
							.getCellLocation();
					lac = gcl.getLac();
				} catch (Exception e) {
					lac = -1;
				}
				return lac;
			}

			public static String getNetworkOperator(Context c) {
				String ret = null;
				try {
					initTelephonyManager(c);
					ret = tm.getNetworkOperator();
				} catch (Exception e) {
					ret = "";
				}
				return ret;
			}
		}
	}

	public static class Msg {
		private static Toast to = null;

		public static void sendMsg(Handler h, int tag, Object obj,
				Integer intvalue) {
			Message msg = new Message();
			msg.what = tag;
			msg.obj = obj;
			if (intvalue != null)
				msg.arg1 = intvalue.intValue();
			h.sendMessage(msg);
		}

		public static void sendMsg(Handler h, int tag, Integer intvalue) {
			Message msg = new Message();
			msg.what = tag;
			if (intvalue != null)
				msg.arg1 = intvalue.intValue();
			h.sendMessage(msg);
		}

		public static void sendMsg(Handler h, int tag, Integer intv1,
				Integer intv2, Object obj) {
			Message msg = new Message();
			msg.what = tag;
			if (intv1 != null)
				msg.arg1 = intv1.intValue();
			if (intv2 != null)
				msg.arg2 = intv2.intValue();
			if (obj != null)
				msg.obj = obj;
			h.sendMessage(msg);
		}

		public static void sendMsg(Handler h, int tag, String msgstr) {
			Message msg = new Message();
			msg.what = tag;
			if (msgstr != null)
				msg.obj = msgstr;
			h.sendMessage(msg);
		}

		public static void sendMsg(Handler h, int tag, Object obj) {
			Message msg = new Message();
			msg.what = tag;
			if (obj != null)
				msg.obj = obj;
			h.sendMessage(msg);
		}

		public static void sendMsg(Handler h, int tag, String msgstr,
				Integer intvalue) {
			Message msg = new Message();
			msg.what = tag;
			if (msgstr != null)
				msg.obj = msgstr;
			if (intvalue != null)
				msg.arg1 = intvalue.intValue();
			h.sendMessage(msg);
		}

		public static void sendMsg(Handler h, int tag) {
			sendMsg(h, tag, null, null, null);
		}

		public static String getMsgString(Message msg) {
			if ((msg != null) && (msg.obj != null))
				return msg.obj.toString();

			return null;
		}

		public static Object getMsgObject(Message msg) {
			if ((msg != null) && (msg.obj != null))
				return msg.obj;

			return null;
		}

		public static Integer getMsgInteger(Message msg) {
			if (msg != null)
				return Integer.valueOf(msg.arg1);

			return null;
		}

		public static Integer getMsgInteger1(Message msg) {
			if (msg != null)
				return Integer.valueOf(msg.arg1);

			return null;
		}

		public static Integer getMsgInteger2(Message msg) {
			if (msg != null)
				return Integer.valueOf(msg.arg2);

			return null;
		}

		public static void showShortText(Context c, String text) {
			Toast.makeText(c, text, 0).show();
		}

		public static void showShortText(Context c, int textResId) {
			Toast.makeText(c, CommonHelper.Resource.getString(c, textResId), 0)
					.show();
		}
	}

	public static class SoftWare {
		public static AppInfo getThisInfo(Context c) {
			AppInfo ret = new AppInfo();
			PackageManager manager = c.getPackageManager();
			PackageInfo info = null;
			try {
				info = manager.getPackageInfo(c.getPackageName(), 0);
				ret.appname = info.applicationInfo.loadLabel(
						c.getPackageManager()).toString();
				ret.pname = info.packageName;
				ret.versionCode = info.versionCode;
				ret.versionName = info.versionName;
				ret.icon = info.applicationInfo.loadIcon(c.getPackageManager());
			} catch (PackageManager.NameNotFoundException e) {
				e.printStackTrace();
				ret = new AppInfo();
			}
			return ret;
		}

		public static AppInfo getThatInfo(Context c, String pname) {
			AppInfo ret = new AppInfo();
			PackageManager manager = c.getPackageManager();
			PackageInfo info = null;
			try {
				info = manager.getPackageInfo(pname, 0);
				ret.appname = info.applicationInfo.loadLabel(
						c.getPackageManager()).toString();
				ret.pname = info.packageName;
				ret.versionCode = info.versionCode;
				ret.versionName = info.versionName;
				ret.icon = info.applicationInfo.loadIcon(c.getPackageManager());
			} catch (PackageManager.NameNotFoundException e) {
				e.printStackTrace();
				ret = new AppInfo();
			}
			return ret;
		}

		public static class AppInfo {
			public String appname;
			public String pname;
			public String versionName;
			public int versionCode;
			public Drawable icon;

			public AppInfo() {
				this.appname = "";
				this.pname = "";
				this.versionName = "";
				this.versionCode = 0;
			}

			public String print() {
				return this.appname + "\t" + this.pname + "\t"
						+ this.versionName + "\t" + this.versionCode + "\t";
			}
		}
	}

	public static class Picture {
		public static Bitmap readBitmap(byte[] _data) {
			return BitmapFactory.decodeByteArray(_data, 0, _data.length);
		}

		public static Bitmap drawableToBitmap(Drawable drawable) {
			if (drawable == null)
				return null;
			Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
					drawable.getIntrinsicHeight(),
					(drawable.getOpacity() != -1) ? Bitmap.Config.ARGB_8888
							: Bitmap.Config.RGB_565);

			Canvas canvas = new Canvas(bitmap);

			drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
					drawable.getIntrinsicHeight());
			drawable.draw(canvas);
			return bitmap;
		}

		public static Bitmap readBitmapSmall(String path, int inSampleSize) {
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inSampleSize = inSampleSize;
			Bitmap bitmap = BitmapFactory.decodeFile(path, options);
			return bitmap;
		}

		public static Bitmap readBitmapSmall(int smallHeight, String path) {
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inJustDecodeBounds = true;

			Bitmap bitmap = BitmapFactory.decodeFile(path, options);
			options.inJustDecodeBounds = false;

			int be = (int) (options.outHeight / smallHeight);
			if (be <= 0)
				be = 1;
			options.inSampleSize = be;

			bitmap = BitmapFactory.decodeFile(path, options);

			return bitmap;
		}

		public static Bitmap readBitmap(String path) {
			Bitmap bitmap = BitmapFactory.decodeFile(path);
			return bitmap;
		}

		public static Bitmap rotate(Bitmap bmap, int degree) {
			Matrix matrix = new Matrix();

			matrix.postRotate(degree);

			Bitmap resizeBitmap = Bitmap.createBitmap(bmap, 0, 0,
					bmap.getWidth(), bmap.getHeight(), matrix, true);
			return resizeBitmap;
		}
	}

	public static class Tran {
		@Deprecated
		public static Bitmap drawableToBitmap(Drawable drawable) {
			return CommonHelper.Picture.drawableToBitmap(drawable);
		}
	}
	public static class MenuName{
		private static java.util.Map<String, String> menuMap;
		public static String match(String key){
			if(menuMap==null){
				menuMap=new HashMap<String, String>();
				menuMap.put("morning_company_task_list", morning_company_task_list);
				menuMap.put("dispatch_company_task_list", dispatch_company_task_list);
				menuMap.put("night_company_task_list", night_company_task_list);
				menuMap.put("morning_branch_task_list", morning_branch_task_list);
				menuMap.put("dispatch_branch_task_list", dispatch_branch_task_list);
				menuMap.put("night_branch_task_list", night_branch_task_list);
				menuMap.put("send_enterprise_task_list", send_enterprise_task_list);
				menuMap.put("receive_enterprise_task_list", receive_enterprise_task_list);
				menuMap.put("bag_verify_list", bag_verify_list);
				menuMap.put("bag_task_list", bag_task_list);
				menuMap.put("system_notice", system_notice);
				menuMap.put("system_notice_read", system_notice_read);
				menuMap.put("modify_pwd", modify_pwd);
			}
			return menuMap.get(key);
		}
		public static final String morning_company_task_list="早送任务";//守押
		public static final String dispatch_company_task_list="中调任务";//守押
		public static final String night_company_task_list="晚接任务";//守押
		
		public static final String morning_branch_task_list="早送任务";//银行客户
		public static final String dispatch_branch_task_list="中调任务";//银行客户
		public static final String night_branch_task_list="晚接任务";//银行客户
		
		public static final String send_enterprise_task_list="送款任务";//企业
		public static final String receive_enterprise_task_list="收款任务";//企业
		public static final String bag_verify_list="箱包交接核对";
		public static final String bag_task_list="箱包交接查询";
		public static final String system_notice="消息发布";
		public static final String system_notice_read="消息通知";
		public static final String modify_pwd="修改密码";
		
	}

	/**
	 * 将用户的权限列表转换成菜单ListView展示的目录
	 * 
	 * @author 吴晓明
	 * @data: 2015-8-28 下午8:33:11
	 * @version: V1.0
	 */
	public static class UserLists {
		public static String[] convertRightsList(String[] systemRights) {
			String[] rightsList = new String[systemRights.length];
			for (int i = 0; i < systemRights.length; i++) {
				String mn=MenuName.match(systemRights[i]);
				if(StringUtils.isEmpty(mn)){
					rightsList[i]=systemRights[i];
				}else {
					rightsList[i]=mn;
				}
			}
			return rightsList;
		}
	}
}